package parser;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.w3c.dom.Document;

import util.Candle;
import util.Util;

public class I3tpParser { 
	
	private HashMap<String, Float> minTargetPrices;
	
	public HashMap<String, Float> getTargetPrices() {
		return minTargetPrices;
	}
	
	public float getTargetPrice(String stockName) {
		if (minTargetPrices.get(stockName) == null) return -1;
		else return minTargetPrices.get(stockName);
	}
	
	public I3tpParser() {
		try {
	        String content = getText("http://sgx.i3investor.com/jsp/pt.jsp"); 
	
	        String regex = ".jsp\" target=\"_top\">([^<>]*)</a>.*?</tr>";
	        Pattern p = Pattern.compile(regex); 
	        Matcher m = p.matcher(content);
	
	        minTargetPrices = new HashMap<String, Float>();
	        while (m.find()) {
	        	String content1 = m.group(0);
	            String regex1 = ">BUY</td>";
	            Pattern p1 = Pattern.compile(regex1); 
	            Matcher m1 = p1.matcher(content1);
	            if (m1.find()) {
	                String regex2 = ">\\d+.\\d+<.*?>(\\d+.\\d+)<";
	                Pattern p2 = Pattern.compile(regex2); 
	                Matcher m2 = p2.matcher(content1);
	                if (m2.find()) {
	                	String stockName = m.group(1);
	                	String matchedStockName = I3.matchStockname(stockName);
	                	float targetPrice = Float.parseFloat(m2.group(1));
	                	if (minTargetPrices.get(matchedStockName) == null) minTargetPrices.put(matchedStockName, targetPrice);
	                	else {
		                	float storedTargetPrice = minTargetPrices.get(matchedStockName);
		                	if (targetPrice < storedTargetPrice) minTargetPrices.put(matchedStockName, targetPrice);
	                	}
	                }
	            }
	        }
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
    private String getText(String url) throws Exception { 
        URL website = new URL(url); 
        URLConnection connection = website.openConnection(); 
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
 
        StringBuilder response = new StringBuilder(); 
        String inputLine; 
 
        while ((inputLine = in.readLine()) != null)  
            response.append(inputLine); 
 
        in.close(); 
 
        return response.toString(); 
    } 
 
    public static void main(String[] args) throws Exception { 
    	I3tpParser i3tpParser = new I3tpParser();

		Iterator<String> iter = i3tpParser.minTargetPrices.keySet().iterator();
		while (iter.hasNext()) {
			String stockName = iter.next();
        	float minTargetPrice = i3tpParser.minTargetPrices.get(stockName);
			String paddedStockName = String.format("%1$-30s", stockName);
       		System.out.println(paddedStockName + "\t" + minTargetPrice);
		}
    } 
} 
